#include"List.h"
ListNode* LTInit()
{
	ListNode* plist = malloc(sizeof(ListNode));
	plist->data = 0;
	plist->next = plist;
	plist->prev = plist;
	return plist;
}
ListNode* LTBuyNode(ListNodetype x)
{
	ListNode* node = malloc(sizeof(ListNode));
	node->data = x;
	node->next = node;
	node ->prev = node;
	return node;
}
void LTPushBack(ListNode* phead, ListNodetype x)
{
	assert(phead);
	ListNode* Newnode = LTBuyNode(x);
	Newnode->next = phead;
	Newnode->prev = phead->prev;
	phead->prev->next = Newnode;
	phead->prev = Newnode;
}
void LTPushFront(ListNode* phead, ListNodetype x)
{
	assert(phead);
	ListNode* Newnode = LTBuyNode(x);
	Newnode->prev = phead;
	Newnode->next = phead->next;
	phead->next->prev = Newnode;
	phead->next = Newnode;
}
void LTPrint(ListNode* phead)
{
	assert(phead);
	ListNode* pcur = phead->next;
	while (pcur!= phead)
	{
		printf("%d -> ", pcur->data);
		pcur = pcur->next;
	}
	printf("\n");
}
bool LTEmpty(ListNode* phead)
{
	assert(phead);
	return phead->next == phead;
}
void LTPopBack(ListNode* phead)
{
	assert(!LTEmpty(phead));
	ListNode* pcur = phead->prev;
	pcur->prev->next = phead;
	phead->prev = pcur->prev;
	free(pcur);
	pcur = NULL;
}
void LTPopFront(ListNode* phead)
{
	assert(!LTEmpty(phead));
	ListNode* pcur = phead->next;
	pcur->next->prev = phead;
	phead->next = pcur->next;
	free(pcur);
	pcur = NULL;
}
ListNode* LTBuyFind(ListNode* phead, ListNodetype x)
{
	assert(phead);
	ListNode* pcur = phead->next;
	while (pcur != phead)
	{
		if (pcur->data == x)
		{
			return pcur;
		}
		pcur = pcur->next;
	}
	return NULL;
}
void LTInset(ListNode* pos, ListNodetype x)
{
	assert(pos);
	ListNode * node = LTBuyNode(x);
	ListNode* pcur = pos->next;
	node->prev = pos;
	node->next = pcur;
	pcur->prev = node;
	pos->next = node;
}
void LTErase(ListNode* pos)
{
	assert(pos);
	pos->next->prev = pos->prev;
	pos->prev->next = pos->next;
	free(pos);
	pos = NULL;
}
void LTDeStory(ListNode* phead)
{
	assert(phead);
	ListNode* pcur = phead->next;
	while (pcur!=phead)
	{
		ListNode* ptail = pcur->next;
		free(pcur);
		pcur = ptail;
	}
	free(phead);
	phead = NULL;
}